package com.mechat.ui.chats.item

import android.app.Activity
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.custom.base.base.BaseRecyclerAdapter
import com.custom.base.config.BaseConfig
import com.github.jdsjlzx.util.SuperViewHolder
import com.mechat.R
import com.mechat.bean.ConversationChatListBean
import com.mechat.util.GlideUtil
import com.mechat.util.HttpRequest
import io.rong.imkit.SpName
import io.rong.imkit.userinfo.RongUserInfoManager
import io.rong.imkit.utils.RouteUtils
import io.rong.imlib.model.Conversation
import io.rong.imlib.model.UserInfo

/**
 */
class ItemAdapter(
    listModel: ArrayList<ConversationChatListBean>,
    mActivity: Activity,
) : BaseRecyclerAdapter<ConversationChatListBean>(listModel, mActivity) {
    override val layoutId: Int get() = R.layout.item_chats

    override fun onBindItemHolder(
        position: Int,
        holder: SuperViewHolder,
        model: ConversationChatListBean,
        payloads: List<Any>?
    ) {
        val itemUserAvatar = holder.getView<ImageView>(R.id.item_user_avatar)
        val itemUserOnline = holder.getView<View>(R.id.item_user_online)
        val itemUserTitle = holder.getView<TextView>(R.id.item_user_title)
        val itemUserContent = holder.getView<TextView>(R.id.item_user_content)
        val itemUserLastTime = holder.getView<TextView>(R.id.item_user_last_time)
        val itemUserUnread = holder.getView<TextView>(R.id.item_user_unread)
        val bottomLine = holder.getView<View>(R.id.bottom_line)
        val itemUserAvatarStroke = holder.getView<View>(R.id.item_user_avatar_stroke)
        val itemUserCrown = holder.getView<ImageView>(R.id.item_user_crown)
        val outSideContainer = holder.getView<ConstraintLayout>(R.id.out_side_container)
        val itemUserLevel = holder.getView<TextView>(R.id.item_user_level)
        GlideUtil.setImageView(model.avatarUrl, itemUserAvatar)
        itemUserCrown.visibility = if (model.member) View.VISIBLE else View.GONE
        itemUserAvatarStroke.visibility = if (model.member) View.VISIBLE else View.GONE
        itemUserOnline.visibility = if (model.online == 1) View.VISIBLE else View.GONE
        itemUserTitle.text = model.nickName
        itemUserContent.text = model.latestMsg
        itemUserLastTime.text = model.latestSendTime
        itemUserUnread.visibility = if (model.unReadNum > 0) View.VISIBLE else View.GONE
        itemUserUnread.text = if (model.unReadNum > 99) "99+" else "${model.unReadNum}"
        bottomLine.visibility = if (position == itemCount - 1) View.GONE else View.VISIBLE
        if (null !=model.level) {
            itemUserLevel.text = "Lv${model.level}"
            when (model.level) {
                0 -> itemUserLevel.setBackgroundResource(R.drawable.shape_user_level_one)
                1, 2, 3 -> itemUserLevel.setBackgroundResource(R.drawable.shape_user_level_two)
                4, 5, 6 -> itemUserLevel.setBackgroundResource(R.drawable.shape_user_level_three)
                7, 8, 9 -> itemUserLevel.setBackgroundResource(R.drawable.shape_user_level_four)
                10 -> itemUserLevel.setBackgroundResource(R.drawable.shape_user_level_five)
                else -> {}
            }
        }
        outSideContainer.setBackgroundColor(
            if (model.toppingFlag == 1) ContextCompat.getColor(
                mActivity,
                R.color.ps_color_eb
            ) else Color.WHITE
        )
        RongUserInfoManager.getInstance()
            .refreshUserInfoCache(UserInfo(
                model.targetId,
                model.nickName,
                Uri.parse(model.avatarUrl)
            ))

        holder.setOnClickListener {
            BaseConfig.getInstance.setString(SpName.modelChatUserCode,model.targetId)
            val bundle = Bundle()
            bundle.putInt("blackFlag",model.blackFlag)
            RouteUtils.routeToConversationActivity(
                mActivity,
                Conversation.ConversationType.PRIVATE,
                model.targetId, bundle
            )
            HttpRequest.commonNotify("UserListClick",model.targetId)
        }
    }
}